package com.itheima.controller;

import com.itheima.entity.ResultInfo;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    @RequestMapping("/login")
    public ResultInfo login(@RequestBody User user, HttpSession session) {
        System.out.println(user + "开始登录");
        //调用业务层实现登录
        User login = userService.login(user.getUsername(), user.getPassword());
        //登录成功
        if (login != null) {
            //将用户信息放在会话域中
            session.setAttribute("user", login);
            return new ResultInfo(true, "登录成功");
        }
        else {
            return new ResultInfo(false, "登录失败");
        }
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public void logout(HttpSession session) {
        session.invalidate();
    }

}
